<?php


    namespace Admin\Event;
    use Think\Controller;

    class CategoryEvent extends Controller
    {
        //添加分类到数据库
        function addTopCategory ()
        {
            if(IS_POST){
                $data['name'] = I("post.name");
                $data['pid'] = I("post.pid");
                $data['path'] = I("post.path");
                $data['create_time'] = date("Y-m-d H:i:s");
                $category = M("category");
                $id = $category -> add($data);
                return $id;
            }
        }

        //产品分类查询顶级分类
        function category ($pid)
        {
            $cate = M("category");
            $pid = empty($pid) ? 0 : $pid;

            $catelist = $cate ->where("pid=$pid and is_show=1") -> select();

            return $catelist;




        }

        //添加子类时,查询该类的path
        function queryfield ()
        {
            $cate = M("category");
            $id = I("get.id");
            //根据id查pid path name
            $cateinfo = $cate->field(array("path","pid","name"))->where("id=$id")->find();
            return $cateinfo;
        }

        //查询子分类
        function queryCategory ()
        {
            if(IS_GET){
                $cate = M("category");
                $pid = I("get.id");
                $catelist = $cate ->where("pid=$pid") -> select();
                if($pid != 0){
                    //根据父类的id查询父类的名称
                    $pname = $cate -> getFieldById($pid,"name");
                }else{
                    $pname = 0;
                }
                foreach($catelist as $key => $value){
                    $catelist[$key]['pname'] = $pname;
                }
                return json_encode($catelist);
            }
        }

        //从商品子分类返回父类,查询父类的信息
        function backCategory ()
        {
            if(IS_GET){
                $cate = M("category");
                $id = I("get.id");
                //根据父类的id查询pid
                $pid = $cate -> getFieldById($id,"pid");
                $catelist = $cate ->where("pid=$pid") -> select();
                if($pid != 0){
                    //根据父类的id查询父类的名称
                    $pname = $cate -> getFieldById($pid,"name");
                }else{
                    $pname = 0;
                }
                //再返回的数组中添加一个属性pname
                foreach($catelist as $key => $value){
                    $catelist[$key]['pname'] = $pname;
                }
                return json_encode($catelist);
            }
        }
        //编辑分类名
        function editCategoryDo ()
        {
            if(IS_POST){
                $data['name'] = I("post.name");
                $id = I("post.id");
                $cate = M("category");
                $res = $cate -> where("id=$id")->save($data);
                if($res){
                    return true;
                }else{
                    return false;
                }
            }
        }
        //删除分类
        function delCategoryDo ()
        {
            //获取要删除的分类id
            $id = I("get.id");
            //1. 删除前要查看该分类是否有商品
            $good = M("goods");
            $goodinfo = $good -> where("category_id=$id")->find();
            //如果没有,则可以删除
            if(empty($goodinfo)){
                //2. 删除前要确认该分类没有子分类
                $cate = M("category");
                $cateinfo = $cate ->where("pid=$id")->find();
                //如果没有,则可以删除
                if(empty($cateinfo)){
                    $row = $cate ->where("id=$id")->delete();
                    if($row){//成功返回true 反之则false;
                        return "删除成功";
                    }else{
                        return "删除失败";
                    }
                }else{
                    //如果有,则不能删除
                    return -1;
                }

            }else{
                //如果有,则不能删除
                return -2;
            }

        }

        //商品分类是否显示切换
        Public function change_category_show()
        {
            if(IS_GET){
                $is_show = I("get.is_show");
                $id = I("get.id");
                //如果显示就变不显示,反之则显示  0不显示  1显示
                if($is_show == 0){
                    $data['is_show'] = 1;
                }else{
                    $data['is_show'] = 0;
                }

                $cate = M("category");
                $row =$cate -> where("id=$id") ->save($data);
                if($row > 0){
                    return $data['is_show'];
                }else{
                    return "切换失败";
                }

            }
        }
    }


















